module SalaEncja where
import Sala

class (ObjectSalas o) => EncjaSala d o where
	empty :: d o
	getLastID :: d o -> SalaID
	getObjects :: d o -> [o]
	setLastID :: SalaID -> d o -> d o
	setObjects :: [o] -> d o -> d o

	insert :: Nazwa -> 	Rows	->	Cols -> d o -> d o
	insert as db = setLastID i' db' where
		db' = setObjects os' db
		os' = o : os
		os = getObjects db
		o = objectSala i' as
		i' = 1 + getLastID db

	select :: SalaID -> d o -> o
	select i = head . filter ((i==).getSalaID) . getObjects

	selectBy :: (o -> Bool) -> d o -> [o]
	selectBy f = filter f . getObjects
	
data ESala o = DB SalaID [o] deriving Show

instance EncjaSala ESala ObjectSala where
	empty = DB 0 []
	getLastID (DB i os) = i
	setLastID i (DB j os) = DB i os
	getObjects (DB i os) = os
	setObjects os (DB i ps) = DB i os